package com.servlet;

import com.Impl.collect_productServiceImpl;
import com.entity.collect_product;
import com.service.collect_productService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class collect_productServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //设置编码
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");
    //拿值
    System.out.println("进入doGET！");
    //拿类型
    String type = req.getParameter("type");
    String page = req.getParameter("page");
    if(type!=null && type.length()!=0){
      if(page.equals("con")){
        switch (type){
          case "add":
            //执行添加
            String cname = req.getParameter("cname");
            double cprice = Double.parseDouble(req.getParameter("cprice"));
            String cpathPic = req.getParameter("cpathPic");
            String cbrief = req.getParameter("cbrief");
            int bid = Integer.parseInt(req.getParameter("bid"));
            collect_product collect_product = new collect_product();
            collect_product.setCid(1);
            collect_product.setBid(bid);
            collect_product.setCname(cname);
            collect_product.setCprice(cprice);
            collect_product.setCpathPic(cpathPic);
            collect_product.setCbrief(cbrief);
            // 执行添加
            collect_productService collect_productServlet = new collect_productServiceImpl();
            boolean flag = collect_productServlet.addCollect_Product(collect_product);
            PrintWriter out = resp.getWriter();
            if(flag){
              out.println("<script>alert('收藏成功!');location.href='myul';</script>");
            }else{
              out.println("<script>alert('已经收藏过啦!!');</script>");
              out.println("<script>alert('收藏失败!');location.href='myul';</script>");
            }
            break;
          case "load":
            //查询全部
            break;
          case "loadAll"://加载公共产品
            collect_productService collect_productServlet2 = new collect_productServiceImpl();
            List<collect_product> list = collect_productServlet2.findALl();
            req.setAttribute("arr2",list);
            req.getRequestDispatcher("公共产品库.jsp").forward(req,resp);
            break;
        }
      }else if(page.equals("index")){
        switch (type){
          case "add":
            //执行添加
            String cname = req.getParameter("cname");
            double cprice = Double.parseDouble(req.getParameter("cprice"));
            String cpathPic = req.getParameter("cpathPic");
            String cbrief = req.getParameter("cbrief");
            int bid = Integer.parseInt(req.getParameter("bid"));
            collect_product collect_product = new collect_product();
            collect_product.setCid(1);
            collect_product.setBid(bid);
            collect_product.setCname(cname);
            collect_product.setCprice(cprice);
            collect_product.setCpathPic(cpathPic);
            collect_product.setCbrief(cbrief);
            // 执行添加
            collect_productService collect_productServlet = new collect_productServiceImpl();
            boolean flag = collect_productServlet.addCollect_Product(collect_product);
            PrintWriter out = resp.getWriter();
            if(flag){
              out.println("<script>alert('收藏成功!');location.href='myul';</script>");
            }else{
              out.println("<script>alert('已经收藏过啦!!');</script>");
              out.println("<script>alert('收藏失败!');location.href='myul';</script>");
            }
            break;
          case "load":
            //查询全部
            collect_productService collect_productServlet2 = new collect_productServiceImpl();
            List<collect_product> list = collect_productServlet2.findALl();
            req.setAttribute("arr2",list);
            req.getRequestDispatcher("我的收藏.jsp").forward(req,resp);
            break;
        }
      }else if(page.equals("shou")){
        switch (type){
          case "load":
            //查询全部
            collect_productService collect_productServlet2 = new collect_productServiceImpl();
            List<collect_product> list = collect_productServlet2.findALl();
            req.setAttribute("arr2",list);
            req.getRequestDispatcher("我的收藏.jsp").forward(req,resp);
            break;
        }
      }else{
        switch (type){
          case "add":
            //执行添加
            String cname = req.getParameter("cname");
            double cprice = Double.parseDouble(req.getParameter("cprice"));
            String cpathPic = req.getParameter("cpathPic");
            String cbrief = req.getParameter("cbrief");
            int bid = Integer.parseInt(req.getParameter("bid"));
            collect_product collect_product = new collect_product();
            collect_product.setCid(1);
            collect_product.setBid(bid);
            collect_product.setCname(cname);
            collect_product.setCprice(cprice);
            collect_product.setCpathPic(cpathPic);
            collect_product.setCbrief(cbrief);
            // 执行添加
            collect_productService collect_productServlet = new collect_productServiceImpl();
            boolean flag = collect_productServlet.addCollect_Product(collect_product);
            PrintWriter out = resp.getWriter();
            if(flag){
              out.println("<script>alert('收藏成功!');location.href='myul';</script>");
            }else{
              out.println("<script>alert('已经收藏过啦!!');</script>");
              out.println("<script>alert('收藏失败!');location.href='myul';</script>");
            }
            break;
          case "load":
            //查询全部
            collect_productService collect_productServlet2 = new collect_productServiceImpl();
            List<collect_product> list = collect_productServlet2.findALl();
            req.setAttribute("arr2",list);
            req.getRequestDispatcher("我的收藏.jsp").forward(req,resp);
            break;
        }
      }
    }

//    System.out.println(cname);
//    System.out.println(cprice);
//    System.out.println(cpathPic);
//    System.out.println(cbrief);
//    System.out.println(bid);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//    doGet(req,resp);
    System.out.println("进入doGET！");
  }
}
